package com.idea.plugin.helper;

import com.intellij.lang.Language;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;


/**
 * Ide动作事件工具类
 *
 * @author maoxikai
 * @date 2018-10-05
 */
public class IdeActionHelper {

    private IdeActionHelper() {
    }

    /**
     * 取得当前ide事件信息
     *
     * @param anActionEvent 动作事件句柄
     * @return
     */
    public static IdeEventInfo getCurrentEvent(AnActionEvent anActionEvent) {
        Project project = anActionEvent.getData(CommonDataKeys.PROJECT);
        Module module = anActionEvent.getData(LangDataKeys.MODULE);
        Project projectContext = anActionEvent.getData(PlatformDataKeys.PROJECT_CONTEXT);
        Module moduleContext = anActionEvent.getData(LangDataKeys.MODULE_CONTEXT);
        VirtualFile virtualFile = anActionEvent.getData(CommonDataKeys.VIRTUAL_FILE);
        PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE);
        Editor editor = anActionEvent.getData(CommonDataKeys.EDITOR);
        String fileText = anActionEvent.getData(PlatformDataKeys.FILE_TEXT);
        Language language = anActionEvent.getData(LangDataKeys.LANGUAGE);
        Boolean isModalContext = anActionEvent.getData(PlatformDataKeys.IS_MODAL_CONTEXT);

        //组织对象
        IdeEventInfo ideEventInfo = new IdeEventInfo();
        ideEventInfo.setProject(project);
        ideEventInfo.setModule(module);
        ideEventInfo.setProjectContext(projectContext);
        ideEventInfo.setModule(moduleContext);
        ideEventInfo.setVirtualFile(virtualFile);
        ideEventInfo.setPsiFile(psiFile);
        ideEventInfo.setEditor(editor);
        ideEventInfo.setFileText(fileText);
        ideEventInfo.setLanguage(language);
        ideEventInfo.setIsModalContext(isModalContext);

        return ideEventInfo;
    }
}
